#pragma once
#include "stdafx.h"
#include "EventListeners.h"
#include <HSModel/Model3D.h>
#include <HSModel/SharedConstants.h>

CEGUI::MouseButton Utilities::ConvertButton(OIS::MouseButtonID buttonID)
  {
  switch (buttonID)
    {
    case OIS::MB_Left:
      return CEGUI::LeftButton;
      break;

    case OIS::MB_Right:
      return CEGUI::RightButton;
      break;

    case OIS::MB_Middle:
      return CEGUI::MiddleButton;
      break;

    default:
      return CEGUI::LeftButton;
      break;
    }
  }

CeguiEventListener::CeguiEventListener(Application& i_application)
  : m_application(i_application)
  {    }

bool CeguiEventListener::KeyPressed(const OIS::KeyEvent &keyEventRef) 
  { 
  CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
  context.injectKeyDown((CEGUI::Key::Scan)keyEventRef.key);
  context.injectChar((CEGUI::Key::Scan)keyEventRef.text);
  return true;
  }

bool CeguiEventListener::KeyReleased(const OIS::KeyEvent &keyEventRef)  
  { 
  CEGUI::System::getSingleton().getDefaultGUIContext().injectKeyUp((CEGUI::Key::Scan)keyEventRef.key);
  CEGUI::System::getSingleton().getDefaultGUIContext().injectChar(keyEventRef.text);
  return true; 
  }

bool CeguiEventListener::MouseMoved(const OIS::MouseEvent &evt) 
  { 
  CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
  context.injectMousePosition(evt.state.X.abs, evt.state.Y.abs);

  m_application.RaiseMoveEvent(evt);

  // Scroll wheel.
  if (evt.state.Z.rel)
    context.injectMouseWheelChange(evt.state.Z.rel / 120.0f);
  return false; 
  }
/*void DrawALine(TVector3 i_origin, TVector3 i_direction) // uses counter for name of a line, that should be added somewhere in this scope
{
std::string s = std::to_string(manual_line_cnt++);
Ogre::ManualObject* manual = m_application. GetSceneController().mp_scene_manager->createManualObject(s);
manual->begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_STRIP);

manual->position(i_origin.X(), i_origin.Y(), i_origin.Z());  // start position
manual->position(i_origin.X() + 1000*i_direction.X(), i_origin.Y() + 1000*i_direction.Y(), i_origin.Z() + 1000*i_direction.Z());  // draw first line

manual->end();
m_application.GetSceneController().mp_scene_manager->getRootSceneNode()->createChildSceneNode()->attachObject(manual);
}*/

bool CeguiEventListener::MousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id) 
  { 
  CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
  context.injectMouseButtonDown(Utilities::ConvertButton(id));
    
  if(m_application.GetMenu().IsMenuHere(evt.state.X.abs, evt.state.Y.abs))
    return false;

  m_application.RaiseClickEvent(evt, id);

  return false; 
  }
bool CeguiEventListener::MouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id) 
  {
  CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
  context.injectMouseButtonUp(Utilities::ConvertButton(id));

  m_application.RaiseMouseUpEvent(evt, id);
  return true;
  }


